import Vue from 'vue'
import Vuex from 'vuex'
import { loginAPI } from '../api/login'

Vue.use(Vuex)

export default new Vuex.Store({
  strict: true,
  state: {
    token: '',
    refreshToken: ''
  },
  mutations: {
    setToken (state, tokenStr) {
      state.token = tokenStr
      window.localStorage.setItem('token', tokenStr)
    },
    setRefeshToken (state, refreshTokenStr) {
      state.refreshToken = refreshTokenStr
      window.localStorage.setItem('refreshToken', refreshTokenStr)
    }
  },

  actions: {
    async asynLoginAction (store, formLogin) {
      const resp = await loginAPI(formLogin)
      const { token, refresh_token: refreshToken } = resp.data.data
      store.commit('setToken', token)
      store.commit('setRefeshToken', refreshToken)
    }
  },
  modules: {
  }
})
